using System;
using System.Windows.Forms;

namespace FreeFlow.WinFormsControls.HelperClasses
{
	/// <summary>
	/// Dialog for arbitrary user input.
	/// </summary>
  public class InputBox : System.Windows.Forms.Form
  {
    private System.Windows.Forms.Button OKBtn;
    private System.Windows.Forms.Button CancelBtn;
    private System.Windows.Forms.TextBox InputEdit;

    /// <summary>
    /// Creates a new <see cref="InputBox"/> instance.
    /// </summary>
    public InputBox()
    {
      InitializeComponent();
    }

    private void InitializeComponent()
    {
      this.InputEdit = new System.Windows.Forms.TextBox();
      this.OKBtn = new System.Windows.Forms.Button();
      this.CancelBtn = new System.Windows.Forms.Button();
      this.SuspendLayout();
      // 
      // InputEdit
      // 
      this.InputEdit.Location = new System.Drawing.Point(8, 8);
      this.InputEdit.Name = "InputEdit";
      this.InputEdit.Size = new System.Drawing.Size(256, 20);
      this.InputEdit.TabIndex = 0;
      this.InputEdit.Text = "";
      // 
      // OKBtn
      // 
      this.OKBtn.DialogResult = System.Windows.Forms.DialogResult.OK;
      this.OKBtn.FlatStyle = System.Windows.Forms.FlatStyle.System;
      this.OKBtn.Location = new System.Drawing.Point(104, 40);
      this.OKBtn.Name = "OKBtn";
      this.OKBtn.TabIndex = 1;
      this.OKBtn.Text = "OK";
      // 
      // CancelBtn
      // 
      this.CancelBtn.DialogResult = System.Windows.Forms.DialogResult.Cancel;
      this.CancelBtn.FlatStyle = System.Windows.Forms.FlatStyle.System;
      this.CancelBtn.Location = new System.Drawing.Point(184, 40);
      this.CancelBtn.Name = "CancelBtn";
      this.CancelBtn.TabIndex = 2;
      this.CancelBtn.Text = "Cancel";
      // 
      // InputBox
      // 
      this.AcceptButton = this.OKBtn;
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.CancelButton = this.CancelBtn;
      this.ClientSize = new System.Drawing.Size(274, 71);
      this.ControlBox = false;
      this.Controls.Add(this.CancelBtn);
      this.Controls.Add(this.OKBtn);
      this.Controls.Add(this.InputEdit);
      this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
      this.Name = "InputBox";
      this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
      this.Text = "InputBox";
      this.ResumeLayout(false);

    }

    /// <summary>
    /// Shows the input box with the specified caption.
    /// </summary>
    /// <param name="caption">Caption.</param>
    /// <returns>The text entered, null if the Cancel button is pressed.</returns>
    public static string ShowInputBox(string caption)
    {
      return ShowInputBox(caption, "");
    }

    /// <summary>
    /// Shows the input box with the specified caption and default text.
    /// </summary>
    /// <param name="caption">Caption.</param>
    /// <param name="defaultText">Default text.</param>
    /// <returns>The text entered, null if the Cancel button is pressed.</returns>
    public static string ShowInputBox(string caption, string defaultText)
    {
      return ShowInputBox(caption, defaultText, false, true);
    }

    /// <summary>
    /// Shows the input box with the specified caption and default text.
    /// </summary>
    /// <param name="caption">The caption.</param>
    /// <param name="defaultText">The default text.</param>
    /// <param name="password">if set to <c>true</c> the input text will be hidden.</param>
    /// <returns>
    /// The text entered, null if the Cancel button is pressed.
    /// </returns>
    public static string ShowInputBox(string caption, string defaultText, bool password)
    {
      return ShowInputBox(caption, defaultText, password, true);
    }

    /// <summary>
    /// Shows the input box with the specified caption and default text.
    /// </summary>
    /// <param name="caption">The caption.</param>
    /// <param name="defaultText">The default text.</param>
    /// <param name="password">if set to <c>true</c> the input text will be hidden.</param>
    /// <param name="showCancel">if set to <c>true</c> the Cancel button will be shown.</param>
    /// <returns>
    /// The text entered, null if the Cancel button is pressed.
    /// </returns>
    public static string ShowInputBox(string caption, string defaultText, bool password, bool showCancel)
    {
      string Answer = null;
      using (InputBox box = new InputBox())
      {
        box.Text = caption;
        if (password)
          box.InputEdit.PasswordChar = '*';
        box.InputEdit.Text = defaultText;
        box.CancelBtn.Visible = showCancel;
        if (box.ShowDialog() == DialogResult.OK)
        {
          Answer = box.InputEdit.Text;
        }
      }
      return Answer;
    }    

  }
}
